define([
	"widget/MapManager",
	"esri/layers/GraphicsLayer",
	"esri/InfoTemplate"
], function(MapManager, GraphicsLayer, InfoTemplate) {
	var graphicLayers = {};
	return {
		addGraphicLayer: function(params) {
			
			var map = params.map, self = this;
			if(map.getLayer[params.id]) {
				console.log("图层id为：" + params.id + "的图层已经存在！");
				return;
			}
			if(!map) {
				map = MapManager.getMap();
			}
			params = params ? params : {};
			var graphicsLayer = new GraphicsLayer(params);
			graphicsLayer.on("mouse-over", function() {
				MapManager.setCursor(map, "pointer");
			});
			graphicsLayer.on("mouse-out", function() {
				MapManager.setCursor(map);
			});
			graphicLayers[graphicsLayer.id] = graphicsLayer;
			if(params.infoTemplate) {
				self.setInfoTemplate(graphicsLayer, params.infoTemplate);
			}
			if(params.graphicClick && typeof params.graphicClick == "function") {
				graphicsLayer.on("click", function(evt) {
					params.graphicClick(map,evt);
				});
			}
			if(params.index || params.index == 0){
				map.addLayer(graphicsLayer,params.index);
			}else{
					map.addLayer(graphicsLayer);
			}
			return graphicsLayer;
		},
		/**
		 * @summary 给图层设置I信息框
		 * @param {GraphicsLayer} graphicsLayer
		 * @param {URL} infoTemplate
		 */
		setInfoTemplate: function(graphicsLayer, infoTemplate) {
			var id = graphicsLayer.id;
			require(["dojo/text!" + infoTemplate], function(content) {
				var infoTemplate = new InfoTemplate("详细信息", content);
				graphicsLayer.setInfoTemplate(infoTemplate);
			});
			
			 $("#mainMap .esriPopup .titleButton.maximize").unbind('click').click(function() {
				  if ($(this).parents(".esriPopup").find(".contentPane table:first").next().is(":hidden")) {
		              $(this).parents(".esriPopup").find(".contentPane table:first").css("display","none");
						$(this).parents(".esriPopup").find(".contentPane table").next().css("display","block");
				  }else{
					  if ($(".sider")) {
		                  $(".sider").remove();
		              }
					  $(this).parents(".esriPopup").find(".contentPane table:first").css("display","block");
                      $(this).parents(".esriPopup").find(".contentPane table").next().css("display","none");
				  }

			  });
			 
		},
		/**
		 * @summary 根据ID，从graphicLayers中获取图层
		 * @param {string} layerId
		 * @return {GraphicLayer}
		 */
		getGraphicLayer: function(layerId,map) {
			if(map.getLayer(layerId)){
				//if(graphicLayers[layerId]) {
					return map.getLayer(layerId);
				//}
			}
			
			return null;
		},
		removeLayer: function(layerId, map) {
			var map = map ? map : MapManager.getMap();
			var graphicLayer = graphicLayers[layerId];
			if(graphicLayer) {
				map.removeLayer(graphicLayer);
				graphicLayers[layerId] = null;
			}

		}
	}

});